<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div id="box" style="color: red;">
			<ul>
				<li class="testLi">11</li>
				<li class="testLi">12</li>
				<li class="testLi">11</li>
				<li class="testLi">12</li>
			</ul>
		</div>
		<div id="pox">
			<ul>
				<li>pox</li>
				<li>pox</li>
				<li>pox</li>
			</ul>
		</div>
		<b class="one-b"></b>
		<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
		<script type="text/javascript">
			(function(window,document) {
				var w = window,
					doc = document;
				
				var Kodo = function(selector) {
					return new Kodo.prototype.init(selector);
				}
				Kodo.prototype = {
					constructor : Kodo,
					length : 0,
					splice: [].splice,
					selector : '',
					init : function(selector) {//dom选择的一些判断
						if(!selector) { return this; }

						if (typeof selector == 'object') {
							for (var i = 0; i < selector.length; i++) {
								this[i] = selector[i];
							}
							this.length = selector.length;
							return this;
						} else if (typeof selector == 'function') {
							Kodo.ready(selector);
							return;
						}
						
						var selector = selector.trim(),
							elm;
						
						if (selector.charAt(0) == '#' && !selector.match('\\s')) {
							selector = selector.substring(1);
							this.selector = selector;
							elm = doc.getElementById(selector);
							
							this[0] = elm;
							this.length = 1;
							return this;
						} else {
							elm = doc.querySelectorAll(selector);
							for (var i = 0; i < elm.length; i++) {
								this[i] = elm[i];
							}
							
							this.selector = selector;
							this.length = elm.length;
							return this;
						}
					},
					css : function(attr,val) {//链式测试
						console.log(this.length);
						for(var i = 0;i < this.length; i++) {
							if(arguments.length == 1) {
								return getComputedStyle(this[i],null)[attr];
							} 
							this[i].style[attr] = val;
						}
						return this;
					},
					hasClass : function(cls) {
						var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
						for (var i = 0; i < this.length; i++) {
							if (this[i].className.match(reg)) return true
								return false;
						}
						return this;
					},
					addClass : function(cls) {
						var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
						for (var i = 0; i < this.length; i++) {
							if(!this[i].className.match(reg))
								this[i].className += ' ' + cls;
						}
						return this;
					},
					removeClass : function(cls) {
						var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
						for (var i = 0; i < this.length; i++) {
							if (this[i].className.match(reg)) 
								this[i].className = this[i].className.replace(' ' + cls,'');
						}
						return this;
					},
					next : function() {
						return sibling(this[0], "nextSibling");
					},
					prev : function() {
						return sibling(this[0], "previousSibling");
					},
					parent : function() {
						var parent = this[0].parentNode;
						parent && parent.nodeType !== 11 ? parent : null;
						var a = Kodo();
							a[0] = parent;
							a.selector = parent.tagName.toLocaleLowerCase();
							a.length = 1;
 						return a;
					},
					parents : function() {
						var a = Kodo(),
							i = 0;
					    while ( (this[0] = this[0][ 'parentNode' ]) && this[0].nodeType !== 9 ) {
					      if ( this[0].nodeType === 1 ) {
					        a[i] = this[0];
					        i++;
					      }
					    }
					    a.length = i;
					    return a;
					}
					
				};
				Kodo.prototype.init.prototype = Kodo.prototype;
				
				//工具方法
				Kodo.ajax = function() { //直接挂载方法  可k.ajax调用
					console.log(this);
				};
				Kodo.ready = function(fn) {

					doc.addEventListener('DOMContentLoaded',function() {
						fn && fn();
					},false);
					doc.removeEventListener('DOMContentLoaded',fn,true);

				};
				Kodo.each = function(obj,callback) {
					var len = obj.length,
						constru = obj.constructor,
						i = 0;
					
					if(constru === w.f) {
						for (; i < len; i++) {
							var val = callback.call(obj[i],i,obj[i]);
							if(val === false) break;
						}
					} else if (isArray(obj)) {
						for (; i < len; i++) {
							var val = callback.call(obj[i],i,obj[i]);
							if(val === false) break;
						}
					} else {
						for( i in obj ) {
							var val = callback.call(obj[i],i,obj[i]);
							if(val === false) break;
						}
					}
					
				};
				
				function sibling(cur, dir) {
				  while ((cur = cur[dir]) && cur.nodeType !== 1) {}
				  return cur;
				}
				function isArray(obj) {
				    return Array.isArray(obj);
				}
				w.f = Kodo;
			})(window,document);
			
			
			var a = f('li');
			var b = {
				'name' : 'haha',
				'age' : 19
			};
			var c = ['a','b','c'];
			
			f.each(a,function(index,item) {
				console.log(index + '----',item);
			});
			f.each(b,function(index,item) {
				console.log(index + '----' +item);
			});
			f.each(c,function(index,item) {
				console.log(index + '----' +item);
			});
			
			f(function() {
				dosomething1();
				dosomething2();
				dosomething3();
			});
			function dosomething1() {
				console.log('do1');
			}
			function dosomething2() {
				console.log('do2');
			}
			function dosomething3() {
				console.log('do3');
			}

			
		</script>
	</body>
</html>
